# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

set pwd [file dirname [file normalize $argv0]]

source ../package_test_autoconf.tcl
package require macports 1.0

array set ui_options {}
#set ui_options(ports_debug)   yes
#set ui_options(ports_verbose) yes
mportinit ui_options

package require portmpkg 1.0
package require portpkg 1.0
source ./library.tcl
macports_worker_init

# mock getportresourcepath, tests should be able to operate without a ports tree
proc getportresourcepath {url {path {}} {fallback yes}} {
    global pwd
    return [file join $pwd $path]
}

test mpkg_main {
    Port mpkg main unit test.
} -constraints {
    darwin
} -setup {
    global os.platform os.major os.arch destpath package.destpath configure.build_arch \
        subport version revision package.flat maintainers description categories \
        supported_archs porturl pwd
    env_init

    file mkdir $destpath
    close [open $destpath/fondu-060102_1.pkg w+]
    file mkdir ${pwd}/port1.0/package
    close [open ${pwd}/port1.0/package/background.tiff w+]
} -body {
    if {[portmpkg::mpkg_main] != 0} {
        return "FAIL: cannot create pkg"
    }
    if {![file exists $destpath/fondu-060102_1.mpkg/Contents/PkgInfo]} {
        return "FAIL: missing PkgInfo file"
    }
    if {![file exists $destpath/fondu-060102_1.mpkg/Contents/Info.plist]} {
        return "FAIL: missing Info.plist file"
    }
    if {![file exists $destpath/fondu-060102_1.mpkg/Contents/Resources]} {
        return "FAIL: missing Resources dir"
    }
    if {![file exists $destpath/fondu-060102_1.mpkg/Contents/Packages/fondu-060102_1.pkg]} {
        return "FAIL: missing pkg file"
    }
    return "Package mpkg successful."

} -cleanup {
    file delete -force $destpath
    global pwd
    file delete -force ${pwd}/port1.0
} -result "Package mpkg successful."


test make_dependency_list {
    Make dependency list unit test.
} -setup {
    global os.platform os.major os.arch destpath package.destpath configure.build_arch \
        subport version revision package.flat maintainers description categories \
        supported_archs porturl
    env_init

    set destination $pwd/destination
} -body {
    set res [portmpkg::make_dependency_list $subport $destination]
    if {[dict size $res] != 0} {
        return "FAIL: unexpectedly long dependency list: $res"
    }
    # indices: 
    # 0 mport
    # 1 version
    # 2 revision

    # FIXME: The mocked port has no dependencies, and make_dependency_list
    # does not include the top level port, so the result is empty.
    #set res_value [dict get $res $some_depname]

    #if {[llength $res_value] != 3 ||
    #    [lindex $res 1] != 060102 ||
    #    [lindex $res 2] != 1} {
    #        return "FAIL: incorrect dependency list entry: $res"
    #}
    return "Dependency list successful."

} -result "Dependency list successful."


# Covered by mpkg_main
# test make_one_package
# test package_mpkg


cleanupTests
